package com.stylefeng.guns.rest.modular.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.modular.applications.service.IMovieService;
import com.stylefeng.guns.rest.modular.auth.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 常规控制器
 *
 * @author *
 * @date 2018-09-29 16:02
 */
@Controller
@CrossOrigin
@RequestMapping("/qsMovie")
public class MovieController {

    @Autowired
    private IMovieService movieService;

    /**
     * 方法名称: list
     * 方法描述:首页每个类型6部电影
     * qweqweqweqweqwe
     *
     * @throws
     */
    @PostMapping(value = "/homeMovies")
    @ResponseBody
    public ResponseEntity homeMovies(LiveMovieVo record) throws Exception {
        try {
            List<LiveMoviesBO> list = movieService.liveMovesByParams(record);
            return ResponseEntity.ok(ResultUtil.success(list));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 方法名称: homeChangeMovies
     * 方法描述:点击换一批查询当前类型6部电影
     *
     * @return Result
     * @throws
     */
    @PostMapping(value = "/homeChangeMovies")
    @ResponseBody
    public ResponseEntity homeChangeMovies(LiveMovieVo record) throws Exception {
        try {
            List<LiveMove> list = movieService.homeChangeMovies(record);
            return ResponseEntity.ok(ResultUtil.success(list));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 方法名称: moviesPage
     * 方法描述: 各个类型分页显示电影按时间倒序
     *
     * @return Result
     * @throws
     */
    @PostMapping(value = "/moviesPage")
    @ResponseBody
    public ResponseEntity moviesPage(LiveMovieVo record) throws Exception {
        PageRows<LiveMove> rows = new PageRows<>();
        ArrayList lists = new ArrayList();
        try {
            List<LiveMove> list = movieService.moviesPage(record);
            // 组装分页数据
            rows = new PageRows<>(record.getCurrentPage(), record.getPageSize(), list);
            int totalPage = rows.getTotalPage();
            if (record.getCurrentPage() > totalPage) {
                rows.setDataList(lists);
            }
            return ResponseEntity.ok(ResultUtil.success(rows));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 方法名称: liveBanners
     * 方法描述: 获取所有广告列表
     *
     * @return Result
     * @throws
     */
    @PostMapping(value = "/liveBanners")
    @ResponseBody
    public ResponseEntity liveBanners(LiveBanner record) throws Exception {
        try {
            List<LiveBanner> liveBanner = movieService.liveBanners(record);
            return ResponseEntity.ok(ResultUtil.success(liveBanner));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    /**
     * 方法名称: liveTypes
     * 方法描述: 所有类型列表
     * 创建时间 2018年9月22日  下午7:37:19
     */
    @PostMapping(value = "/liveTypes")
    @ResponseBody
    public ResponseEntity liveTypes() throws Exception {
        try {
            List<LiveType> liveTypes = movieService.liveTypes();
            return ResponseEntity.ok(ResultUtil.success(liveTypes));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 方法名称: liveSeries
     * 方法描述: 影视系列列表
     * 创建时间 2018年9月22日  下午7:41:24
     */
    @PostMapping(value = "/liveSeries")
    @ResponseBody
    public ResponseEntity liveSeries(LiveSeries record) throws Exception {
        PageRows<LiveSeries> rows = new PageRows<>();
        ArrayList lists = new ArrayList();
        try {
            List<LiveSeries> list = movieService.selectLiveSeries();
            // 组装分页数据
            rows = new PageRows<>(record.getCurrentPage(), record.getPageSize(), list);
            int totalPage = rows.getTotalPage();
            if (record.getCurrentPage() > totalPage) {
                rows.setDataList(lists);
            }
            return ResponseEntity.ok(ResultUtil.success(rows));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 更新电影观看次数
     *
     * @param movieId
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/updateWatchCount")
    @ResponseBody
    public ResponseEntity updateWatchCount(Integer movieId) throws Exception {
        JSONObject json = new JSONObject();
        try {
            movieService.updateWatchCount(movieId);
            return ResponseEntity.ok(ResultUtil.success(true));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }


    /**
     * 获取分享内容
     *
     * @param record
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/selectByParams")
    @ResponseBody
    public ResponseEntity selectByParams(MovieShare record) throws Exception {
        try {
            MovieShare movieShare = movieService.selectByParams(record);
            return ResponseEntity.ok(ResultUtil.success(movieShare));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    /**
     * 根据手机类型ios Android获取版本信息
     *
     * @param type
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/versionByType")
    @ResponseBody
    public ResponseEntity versionByType(@RequestParam(value = "type", required = true) String type) throws Exception {
        try {
            AppVersion appVersion = movieService.selectByType(type);
            return ResponseEntity.ok(ResultUtil.success(appVersion));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }

    //根据code查询精品
    @PostMapping(value = "/seriesRecords")
    @ResponseBody
    public ResponseEntity seriesRecords(MovieSeriesRecord record) {
        try {
            List<MovieSeriesRecord> movieSeriesRecords = movieService.selectByParams(record);
            return ResponseEntity.ok(ResultUtil.success(movieSeriesRecords));
        } catch (Exception e) {
            return ResponseEntity.ok(ResultUtil.error(e.getMessage()));
        }
    }





}
